/**
 *
 *
 * @author TOTORO
 * @date 2021/6/16 14:07
 */

var $nowEl = jQuery;
var $nowSelect = jQuery;
var tipMsg = "", tipEl = jQuery;
var nowSkuText = "";

    // 标题输入计算
    $("#inputTitle").keyup(function () {
        $(".next-input-len").text($(this).val().length+"/"+60);
    })

    // 自定义属性-点击添加属性，增加输入框
    $(".add-item-btn").click(function () {
        var $sci = $("<div class='sell-custom-item'>" +
            "<span class='text-wrap'><span class='sell-o-input text'>" +
            "<span class='input-wrap'><span class='next-input next-input-single next-input-medium fusion-input'>" +
            "<input type='text' placeholder='属性名称' value='' maxlength='20' height='100%'>" +
            "</span></span></span></span>" +
            "<div class='value-wrap single'>" +
            "<p class='value-split'>:</p>" +
            "<div class='sell-custom-prop-value-box'><span class='sell-o-input fusion-input value-box-input'>" +
            "<span class='input-wrap'><span class='next-input next-input-single next-input-medium clear fusion-input'>" +
            "<input type='text' placeholder='属性值' value='' maxlength='40' height='100%'></span></span></span></div></div>" +
            "<div class='action-wrap'><button type='button' class='next-btn next-btn-text next-btn-normal next-btn-medium remove-btn'>移除</button></div></div>");
        $(this).before($sci);
        // 获取所有输入框
        var $input = $(".sell-custom-item");
        if($input.length > 4){
            $(this).addClass("layui-btn-disabled").css({"color":"#ccc","borderColor":"#dcdee3"}).prop("disabled", true);
        }
    });

    // 自定义标签-点击移出，删除输入框
    $(".sell-custom-property").on("click", ".remove-btn", function () {
        $(this).parent().parent().remove();
        // 还原按钮
        $(".add-item-btn").removeClass("layui-btn-disabled").css({"color":"#222","borderColor":"#c4c6cf"}).prop("disabled", false);
    })

    /**
     * 打开下拉框
     */
    $(".all-items").find(".next-select").click(function (event) {
        $nowEl = $(this);
        // 从数据库获取该id对应数据
        var id = $(this).data("akid");
        var $x = $(this).offset().left;
        var $y = $(this).offset().top;
        // 获取当前下拉框的高度和宽度
        var $width = $(this).width();
        var $height = $(this).height();

        if ($(this).attr("data-status") == 'open') {
            // 改变边框颜色，代表关闭了下拉框
            $(this).find(".next-icon").removeClass("icon-new_shangjiantou").addClass("icon-new_xiajiantou");
            $nowSelect.remove();
            $(this).attr("data-status", "close");
        } else {
            // 改变边框颜色，代表打开了下拉框
            $(this).find(".next-select-inner").css("borderColor", "#2192d9");
            // 将其他边框设为灰色
            $(this).parent().parent().parent().siblings().find(".next-select-inner").css("borderColor", "#c4c6cf");
            // 删除其他的下拉框
            $(".next-select-popup-wrap").remove();
            $(this).parent().parent().parent().siblings().find(".next-select-trigger").attr("data-status", "close");
            $(this).find(".next-select-arrow").find("i").removeClass("icon-new_xiajiantou").addClass("icon-new_shangjiantou");
            $(this).parent().parent().parent().siblings().find(".next-select-arrow").find("i").removeClass("icon-new_shangjiantou").addClass("icon-new_xiajiantou");
            $.ajax({
                type: "POST",
                url: "${pageContext.request.contextPath}/attr/get_spu_value.htm",
                async: false, // 是否异步处理
                data: {'key': id},
                success: function (rs) {
                    var json = eval("(" + rs + ")");
                    $("body").append(searchBox($x, $y, $width, $height));
                    $nowSelect.slideDown(1000);
                    for (let i = 0; i < json.length; i++) {
                        $nowSelect.find("#options").append($("<div class='options-item' title='" + json[i] + "'>" + json[i] + "</div>"));
                    }

                    // 搜索框焦点样式
                    $(".next-small input").focus(function () {
                        $(this).parent().css("borderColor", "#2192d9");
                    }).blur(function () {
                        $(this).parent().css("borderColor", "#c4c6cf");
                    })

                    $(".next-small input").focus();

                    // 选项悬停样式 / 选项点击事件
                    $(".options-item").mouseenter(function () {
                        $(this).css("background", "#F2F3F7").siblings().css("background","#FFF");
                    }).click(function () {
                        if($nowEl.find("input").val().length<1) {
                            // 如果是必填项，并且之前没选择过值，就在必填提示中加1
                            if ($nowEl.data("ismf") == 1) {
                                var nowVal = parseInt($(".now").attr("data-value")) + 1
                                $(".now").attr("data-value", nowVal);
                                $(".now").text("[ " + nowVal + " ]")
                            }
                        }
                        // 将选项的值放在输入框中
                        $nowEl.find("input").val($(this).text());
                        // 删除下拉框
                        $nowSelect.remove();
                        // 改变状态打开状态
                        $nowEl.find(".next-icon").removeClass("icon-new_shangjiantou").addClass("icon-new_xiajiantou");
                        $nowEl.attr("data-status", "close");
                        // 将状态设为拥有值
                        $nowEl.parents(".sell-catProp-item").attr("data-havevalue", true);
                    });


                }
            });

            $(this).attr("data-status", "open");
            event.stopPropagation();
        }
    });

/**
 * 选择输入框输入事件
 */
$(".next-select-trigger-search input").keyup(function () {
    if($(this).val().length>0){
        $(this).parents(".sell-catProp-item").attr("data-havevalue", true);
    } else{
        $(this).parents(".sell-catProp-item").attr("data-havevalue", false);
    }
})

// 点击除了本身的任何地方，关闭下拉框
    $(document).on('click', function (e) {
        var e = e || window.event; //浏览器兼容性
        var elem = e.target || e.srcElement;
        while (elem) { //循环判断至跟节点，防止点击的是div子元素
            if (elem.id && elem.id =='test1') {
                return;
            }
            elem = elem.parentNode;
        }
        // $('#test').css('display', 'none'); //点击的不是div或其子元素
        try {
            $nowEl.find(".next-icon").removeClass("icon-new_shangjiantou").addClass("icon-new_xiajiantou");
            $(".sell-o-select-popup-overlay").remove();
            $nowEl.attr("data-status", "close");
            $nowEl.find(".next-select-inner").css("borderColor", "#c4c6cf");
        } catch (err){
            return;
        }
    });


/**
 * 获得输入框
 * @param x
 * @param y
 * @param width
 * @param height
 * @param json
 * @returns {Window.jQuery|HTMLElement|*|(function(*=, *=): *)|(function(*=, *=): *)|(function(*=, *=): *)|(function(*=, *=): *)}
 */
function searchBox(x, y, width, height, json) {
    $nowSelect = $("<div class='next-overlay-inner next-select-popup-wrap sell-o-select-popup-overlay' style='position: absolute; left: "+x+"px; top: "+(parseInt(y)+parseInt(height))+"px; z-index: 9999; width: "+width+"px;'>"+
        "<div class='sell-o-select-options' id='test1'>"+
        "<div class='options-search'>"+
        "<span class='next-input next-small'>"+
        "<input height='100%'>"+
        "</span>"+
        " <i class='iconfont icon-ct-sousuo' style='display: inline-block'></i>"+
        "</div>"+
        "<div class='options-content'>"+
        "<div style='position: relative; height: 28px;'>"+
        "<div style='transform: translate(0px, 0px);'>"+
        "<div id='options'>"+
        "</div>"+
        "</div>"+
        "</div>"+
        "</div>"+
        "</div>"+
        "</div>");
    return $nowSelect;
}




/**
 * 复选框选择 - 点击设置，根据模板和后台数据生成一个选择的DIV
 */
$(".setUp").click(function () {
    var that = $(this);
    // 用于判断是打开了哪个复选框
    $(this).attr("data-open",true);
    // 设置不可操作body
    $("body").css({
        "overflow-x":"hidden",
        "overflow-y":"hidden",
        "padding-right":"17px"
    });

    // 获取akid
    var akid = $(this).parent().data("akid");
    $.get("/attr/get_spu_value.htm?key="+akid, function (data) {
        generateCheckboxBox().appendTo("body");
        var json = JSON.parse(data);
        for (let i = 0; i < json.length; i++) {
            var $items = $("<div class='next-checkbox-wrapper left-ac' data-value='"+json[i]+"'>\n" +
                "<span class='next-checkbox'>\n" +
                "<input type='checkbox' lay-filter='left-ac' lay-skin='primary' value='"+json[i]+"'>\n" +
                "</span>\n" +
                "<span class='next-checkbox-label'>"+json[i]+"</span>\n" +
                "</div>").appendTo(".sell-o-custom-base-checkbox");
        }
        $("#tck").slideDown(300);
        // 获取之前选中的信息 -- 判断展示信息的div是否隐藏，如果隐藏，则代表没有选中复选框信息
        var flag = that.next().css("display")=='block'?true:false;
        if(flag){
            var info = that.next().text();
            // 根据逗号分割字符串
            var infos = info.toString().split(",");
            // 遍历所有生成的左选框，将拥有上面字符串的选框勾选上
            for (let i = 0; i < infos.length; i++) {
                $(".left-ac").each(function (el, index) {
                    var val = $(this).data("value");
                    if(val == infos[i]){
                        $(this).find("input").prop("checked", true);
                        // 将该复选框放到右选框
                        getRightCheckbox($(this).data("value")).appendTo("#right-group");
                    }
                })
            }
            $(".selected-number").text("已选"+infos.length+"个");
        }



        var form = layui.form;
        form.render('checkbox');
    });
})


// 复选框选择 - 点击确定，将选中的值传回去
$(document).on("click", "#yes", function(){
    // 还原body
    $("body").attr("style", "");

    //获取复选框选中的值
    var strs = "";
    $(".right-ac").each(function (el, index) {
        strs+=$(this).data("value")+",";
    });
    // 逗号最后出现的位置，删除最后一个逗号
    var ps = strs.lastIndexOf(",");
    strs = strs.substring(0, ps);
    $(".next-overlay-wrapper").remove();
    // 如果用户选中了：
    if(strs.length>0){
        $(".setUp[data-open='true']").next().show().text(strs);
        $(".setUp[data-open='true']").find("span").text("修改");
        // 设置为有值的状态
        $(".setUp[data-open='true']").parents(".sell-catProp-item").attr("data-havevalue", true);

        $(".setUp[data-open='true']").attr("data-open",false);
        return;
    }
    // 隐藏展示框
    $(".setUp[data-open='true']").next().hide();
    // 设置为无值的状态
    $(".setUp[data-open='true']").parents(".sell-catProp-item").attr("data-havevalue", false);

    $(".setUp[data-open='true']").attr("data-open",false);
    $("#setUp").find("span").text("设置");
});

$(document).on({
    click: function () {
        $("#tck").slideUp(300, function () {
            $(".next-overlay-wrapper").remove();
            // 还原body
            $("body").attr("style", "");
            $(".setUp[data-open='true']").attr("data-open",false);
        });
    }
}, ".next-dialog-close,#no");


layui.use(['layer', 'form'], function(){
    var form = layui.form;

    // 操作左边选框
    form.on('checkbox(left-ac)', function(data){
        var ck = data.elem; //得到checkbox原始DOM对象
        var flag = data.elem.checked; //是否被选中，true或者false
        var val = data.value; //复选框value值，也可以通过data.elem.value得到

        if(flag) {
            // 添加这个元素
            getRightCheckbox(val).appendTo("#right-group");
        } else{
            // 删除这个元素
            $(".right-ac[data-value='"+val+"']").remove();
        }
        $(".selected-number").text("已选"+$(".right-ac").length+"个");
        form.render('checkbox');
    });

    // 操作右边的复选框
    form.on('checkbox(right-ac)', function (data) {
        var ck = data.elem; //得到checkbox原始DOM对象
        var flag = data.elem.checked; //是否被选中，true或者false
        var val = data.value; //复选框value值，也可以通过data.elem.value得到
        // 删除右边元素
        $(".right-ac[data-value='"+val+"']").remove();
        //将左边元素设为未选中
        $(".left-ac[data-value='"+val+"']").find("input").prop("checked", false);

        $(".selected-number").text("已选"+$(".right-ac").length+"个");
        form.render('checkbox');
    });

    // 全选操作
    form.on('checkbox(selectAll)', function (data) {
        var ck = data.elem; //得到checkbox原始DOM对象
        var flag = data.elem.checked; //是否被选中，true或者false
        var val = data.value; //复选框value值，也可以通过data.elem.value得到
        if(flag){
            $(".left-ac").find("input").each(function (el, index) {
                // 只有未选中才能全选，选中
                if(!$(this).prop("checked")){
                    $(this).prop("checked", true);
                    getRightCheckbox($(this).val()).appendTo("#right-group");
                }
            });
        } else{
            $(".left-ac").find("input").each(function (el, index) {
                $(this).prop("checked", false);
                $(".right-ac[data-value='"+$(this).val()+"']").remove();
            });
        }
        $(".selected-number").text("已选"+$(".right-ac").length+"个");
        form.render('checkbox');
    });
});

/**
 * 获得右边的单个复选框元素
 * @param val
 * @returns {Window.jQuery|HTMLElement|*}
 */
function getRightCheckbox(val) {
    return $("<div class='next-checkbox-wrapper checked right-ac' data-value='"+val+"'>\n" +
        "<input type='checkbox' lay-filter='right-ac' lay-skin='primary' checked='checked' value='"+val+"'>" +
        "<div class='layui-unselect layui-form-checkbox' lay-skin='primary'><i class='layui-icon layui-icon-ok'></i></div>\n" +
        "<span class='next-checkbox-label'>"+val+"</span>\n" +
        "</div>");
}


/**
 * 点击小复选框，触发父类拥有值
 */
$(".small-checkbox-item input").change(function () {
})

layui.use(['layer', 'form'], function() {
    var form = layui.form;
    form.on('checkbox(small-checkbox)', function (data) {
        // 获取和它在同一级的所有复选框
        var $allCk = $(this).parent().parent().find("input:checked");
        if($allCk.length > 0){
            $(this).parents(".sell-catProp-item").attr("data-havevalue", true);
        } else{
            $(this).parents(".sell-catProp-item").attr("data-havevalue", false);
        }
    });

});

/**
 * 生成复选框盒子
 * @returns {*|Window.jQuery|HTMLElement}
 */
function generateCheckboxBox() {
    return $("<div class='next-overlay-wrapper opened''>\n" +
        "<div class='next-overlay-backdrop' id='sym'></div>\n" +
        "    <div class='next-dialog next-closeable next-overlay-inner sell-o-custom-dlg-checkbox-dlg' id='tck'\n" +
        "         style='left: 520px; top: 98px; display: none'>\n" +
        "        <div class='next-dialog-header' id='dialog-title-kpgkv2f2'>请选择</div>\n" +
        "        <div class='next-dialog-body'>\n" +
        "            <div class='checkbox-content'>\n" +
        "                <span  class='next-search next-search-simple next-normal next-medium checkbox-search'>\n" +
        "                    <span class='next-input-group next-search-left'>\n" +
        "                        <span class='next-select next-select-auto-complete next-size-medium next-search-input'>\n" +
        "                            <span class='next-input next-medium'>\n" +
        "                <input role='searchbox' aria-label='搜索' placeholder='搜索'  value='' height='100%'>\n" +
        "                <span class='next-input-inner next-after'>\n" +
        "                    <i role='button' aria-label='搜索' class='next-icon next-icon-search next-medium next-search-icon next-search-symbol-icon'></i>\n" +
        "                </span>\n" +
        "                            </span>\n" +
        "                            <span class='next-sr-only' ></span>\n" +
        "                        </span>\n" +
        "                    </span>\n" +
        "                </span>\n" +
        "\n" +
        "                <div class='select-all'><div class='next-checkbox-wrapper layui-form'>\n" +
        "                    <input type='checkbox'  lay-filter='selectAll' lay-skin='primary' value=''>\n" +
        "                </div>\n" +
        "                    <span class='select-all-label'>全选</span>\n" +
        "                </div>\n" +
        "\n" +
        "                <div class='checkbox-items layui-form'>\n" +
        "                    <div class='sell-o-custom-base-checkbox'>\n" +
        "                        </div>\n" +
        "                </div>\n" +
        "\n" +
        "                <div class='actions'><button type='button' class='next-btn next-medium next-btn-normal' id='no'>\n" +
        "                    <span class=''>取消</span>\n" +
        "                </button> &nbsp;&nbsp;\n" +
        "                    <button type='button' class='next-btn next-medium next-btn-primary' id='yes'>\n" +
        "                        <span class='next-btn-helper'>确定</span>\n" +
        "                    </button>\n" +
        "                </div>\n" +
        "            </div>\n" +
        "            <div class='sell-o-custom-dlg-checkbox-dlg-result'>\n" +
        "                <div class='selected-number'>已选</div>\n" +
        "                <span class='next-checkbox-group next-checkbox-group-hoz layui-form' id='right-group'>\n" +
        "                </span>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "        <div class='next-dialog-footer next-align-right'>\n" +
        "            <button type='button' class='next-btn next-medium next-btn-primary next-dialog-btn' >\n" +
        "                <span class='next-btn-helper'>确认</span>\n" +
        "            </button>\n" +
        "            <button type='button' class='next-btn next-medium next-btn-normal next-dialog-btn'>\n" +
        "                <span class='next-btn-helper'>取消</span>\n" +
        "            </button>\n" +
        "        </div>\n" +
        "        <a role='button' aria-label='关闭' class='next-dialog-close'>\n" +
        "            <i class='iconfont icon-close'></i>\n" +
        "        </a>\n" +
        "    </div>\n" +
        "</div>");
}

// sku操作
layui.use(['layer', 'form'], function(){
    var form = layui.form;
/**
 * 触碰焦点，产生颜色选择器
 */
$(".info-content").on("focus", ".input-with-warning[data-ismf='false']", function () {
    $(this).attr("aria-expanded","true");
    var $x = $(this).offset().left;
    var $y = $(this).offset().top;
    // 生成颜色盒子
    generateColorBox($x, $y).appendTo("body");
    $(".next-overlay-wrapper").show(200);
}).on("blur", ".input-with-warning", function () {
    var that = $(this);
    // 延迟0.2秒关闭，因为选择还未完成
    setTimeout(function () {
        $(".next-overlay-wrapper").hide(200);
        that.attr("aria-expanded","false");
        setTimeout(function () {
            $(".next-overlay-wrapper").remove();
        },200)
    },200);
});

    /**
     * 悬浮，切换颜色
     */
    $("body").on("mouseenter", ".color-group-panel li:not(.active)", function () {
    $(this).addClass("active").siblings().removeClass("active");
    $(".color-list").html("");
    var nowColor = $(this).find("span").text();
    // 获取颜色json
    $.ajaxSettings.async = false;
    $.get("/static/json/color.json", function (data) {
        for (let i = 0; i < data.length; i++) {
            if(data[i]['color'] == nowColor){
                for (let j = 0; j < data[i]['detail'].length; j++) {
                    // 追加
                    var $el = $("<li class='color-tag'>\n" +
                        "        <a><div class='color-block' style='background: rgb("+data[i]['detail'][j]['rgb']+") none repeat scroll 0% 0%;'></div><span>"+data[i]['detail'][j]['name']+"</span></a>\n" +
                        "        </li>").appendTo($(".color-list"));
                }
            }
        }
    })
});

// 选择新的确认颜色操作
$("body").on("click", ".color-list li", function () {
    // 判断下方的复选框是否都选择至少一个才生成
    var $other = $(".other-slt");
    var isAllChecked = true;

    for (let i = 0; i < $other.length; i++) {
        var checkedLength = $other.eq(i).find("input:checked").length;
        if(checkedLength < 1){
            isAllChecked = false;
            break;
        }
    }
    if(!isAllChecked){
        return false;
    }

    // 让表格显示 暂时 ----------------------------------------------------
    $("#struct-sku").show();

    // 获取当前选择颜色的框，填值
    var nowText = $(this).find("span").text();
    $(".input-with-warning[aria-expanded='true']").find("input").val(nowText);
    // 给该颜色选框选上复选框 （代表使用该颜色，用户可取消勾选）
    $(".input-with-warning[aria-expanded='true']").parent().children(":eq(0)").append("<input type='checkbox' lay-filter='color-cb' name='c' lay-skin='primary' checked/>");
    // 改变填充状态为已填充
    $(".input-with-warning[aria-expanded='true']").attr("data-ismf",true);
    form.render('checkbox');
    // 生成一个新的颜色选择框供用户选择
    var colorInput = generateColorInput().appendTo($(".sell-color-checkbox ul"));


    // 判断其他属性类目是否已选择，如选择，则生成规格表
    // 1、获取除颜色以外其他需要选择的类目，因为动态，没有固定性
    var checkedCounts = new Array();
    var $skus = $("#struct-p-1-color").siblings("div[id*='struct-p-']");
    $skus.each(function (index, el) {
        // 将所有的类目属性填写放在数组里
        checkedCounts.push($(this).find("input[type='checkbox']:checked").length);
    });
    // 循环判断是否所有类目属性都是否都填写
    for (let i = 0; i < checkedCounts.length; i++) {
        // 如果有不等于的，直接返回
        if(checkedCounts[i]<1){
            return false;
        }
    }

    var colorNum = $(".sell-color-checkbox-item").length - 2;
    // 生成表格行 tr
    var $nowSkuTr = generateSkuTr(colorNum).appendTo($(".sell-sku-body-table tbody"));
    // 扩大表格div
    // $(".stretch").css("height",(parseInt($(".stretch").css("height")) + 34)+"px");
    // 将已有的类目数据添加在表格行中 1、颜色 颜色独一无二
    var $colorTd = generateSkuTd(nowText).attr("data-type","big");
    $nowSkuTr.find(".sell-sku-cell-image").before($colorTd);
    // 获取所有选中属性里最多的长度
    var maxValue = Totoro.getMaxValue(checkedCounts);
        // 生成第二个的对应的表格行 - 列
        var thisLength = $(this).find("input[type='checkbox']:checked").length;
        // 新建数组存放Tr，方便Tr追加元素，先追加第一个必备的颜色tr
        var $Trs = new Array($nowSkuTr);
        // 因为新建了一个，所以长度减一
    var trLength = (maxValue.length>1?(maxValue[0] * maxValue[1]):maxValue[0]);
        for (let i = 0; i < trLength-1; i++) {
            var $newTr = generateSkuTr(colorNum).appendTo($(".sell-sku-body-table tbody"));
            $Trs.push($newTr);
        }


    $colorTd.attr("rowspan",trLength);
    test($skus, $Trs);

});

    /**
     * *生成属性行 列 for3赋值 取值一样*
     * @param els
     * @param trs
     */
    function test(els, trs) {
    $(els).each(function (index, el) {
        var $thisCheckbox = $(el).find("input[type='checkbox']:checked");
        var splitCount = 0;
        var beforeLength = 0;
        var ckIndex = 0;
        if(index == 0){
            beforeLength = $("#struct-p-1-color").find("input[type='checkbox']:checked").length;
        } else{
            beforeLength = (els).eq(index-1).find("input[type='checkbox']:checked").length
        }
        for (let i = 0; i < beforeLength; i++) {
            for (let j = 0; j < $thisCheckbox.length; j++) {
                var rowCount = $(els).eq(index+1).find("input[type='checkbox']:checked").length;
                if($(els).eq(index+1).find("input[type='checkbox']:checked").length==0){
                    rowCount = 1;
                }
                var $newTd = generateSkuTd($thisCheckbox.eq(j).data("value"));
                // 添加在价格td之前，rowspan为下一个类目的复选框数量
                $newTd.attr("rowspan", rowCount);
                $($(trs)[splitCount]).find(".sell-sku-cell-image").before($newTd);
                // 如果下一个类目属性的复选框不为空的话 （主要是识别不存在的复选框==0） 就根据下一个每个属性都有的复选框数量将tr的下标逐渐加上去 ，因为这个td的rowspan是下一个的复选框数量，所以需要增加
                // 否则的话，就自增，自增代表这个类目的下一个类目不存在，所以每个tr都需要增加td
                if($(els).eq(index+1).find("input[type='checkbox']:checked").length!=0) {
                    splitCount += $(els).eq(index+1).find("input[type='checkbox']:checked").length;
                } else{
                    splitCount++;
                }
            }
        }
    })
}

// 取消勾选复选框，删除整个颜色选框
    form.on('checkbox(color-cb)', function (data) {
        $(this).parent().parent().parent().remove();
    });

    /**
     * 点击添加自定义选框
     */
    $(".confirm-btn").click(function () {
        var $checkboxBox = $(this).parent().next().find(".sequential-checkbox-wrap").children();
        var attrValue = $(this).prev().find("input").val();
        if(attrValue.length<1){
            alert("请输入！");
            return false;
        }
        var $checkbox = generateByValue(attrValue).appendTo($checkboxBox);
        $(this).prev().find("input").val("");
        refreshForm();
    })
});

/**
 * 离开表格td money,自动填充一口价与数量
 */
$(".sell-sku-table-wrapper").on("blur", ".sell-sku-cell-money input", function () {
    // 判断是否输入
    if($(this).val().length<=0) {
        return false;
    }

    // 判断是否有小数点
        if(!Totoro.contain($(this).val(), ".")){
            $(this).val($(this).val()+".00");
        }
    // 在所有价格中获取最小的
    var moneyArray = new Array();
    $(".sell-sku-cell-positiveNumber input").each(function () {
        if($(this).val() != "0") {
            moneyArray.push($(this).parents(".sell-sku-cell-positiveNumber").prev().find("input").val());
        }
    })

    Totoro.getMinValue(moneyArray);
    // 给一口价赋值
    $("#flatlyPrice").val(moneyArray[0]);
});

/**
 * 离开表格td count 修改数量
 */
$(".sell-sku-table-wrapper").on("blur", ".sell-sku-cell-positiveNumber input", function () {
    var count = 0;
    $(".sell-sku-cell-positiveNumber input").each(function (){
        var v = 0;
        if($(this).val().length>0){
            v = parseInt($(this).val());
        } else{
            v = 0;
        }
        count+=v;
    });
    $("#count").val(count);
});


// 点击除了颜色下方的复选框，生成或添加表格数据
$(".other-slt input").click(function () {
});

$("body").on("focus", ".inputPrimary", function () {
    nowSkuText = $(this).val();
})
// 输入颜色别名时，更改表格的名称
$("body").on("keyup", ".inputPrimary", function () {
    var index = $(this).parents(".sell-color-checkbox-item").index();
    var $allTr = $("#struct-sku").find("tr");
    var $allCheckedEl = $(".other-slt");
    var lengthArray = new Array();
    $allCheckedEl.each(function (index, el) {
        var nowLength = $(this).find("input:checked").length;
        lengthArray.push(nowLength);
    })
    var maxLen = Math.max(lengthArray);
    if(index  < 1){
        $allTr.eq(index).children(":eq(0)").find(".sell-sku-cell-text-content").text($(this).val());
    } else{
        $allTr.eq(index+(maxLen-1)).children(":eq(0)").find(".sell-sku-cell-text-content").text($(this).val());
    }
})

// 主要颜色备注输入
$("body").on("keyup", ".comment", function () {
    console.log(123)
    var commentText = $(this).val();
    $(this).parent().parent().parent().prev().find("input").attr("data-comment", commentText);
});

// 批量填充判断
$("#batchFill").click(function () {
    // 只填充价格和数量，判断是否填写了价格和数量
    // 获取是否有价格
    var price = $("#batchPrice").val();
    if(price.length > 0 && Totoro.finalIsNumber(price)){
        $(".sell-sku-cell-money").find("input").val(price+".00");
        // 填写一口价
        $("#flatlyPrice").val(price+".00");
    }
    $("#batchPrice").val("");

    // 获取数量
    var count = $("#batchCount").val();
    if(count.length > 0 && Totoro.finalIsNumber(count)){
        $(".sell-sku-cell-positiveNumber").find("input").val(count);
        // 填写总数量
        // 获得tr的数量
        var length = $(".sku-table-row").length;
        $("#count").val(parseInt(length)*parseInt(count));
    }
    $("#batchCount").val("");
})


/**
 * 生成颜色盒子
 * @returns {*|Window.jQuery|HTMLElement}
 */
function generateColorBox(x,y) {
    return $("<div class='next-overlay-wrapper opened' style='display: none'>\n" +
        "    <div class='next-overlay-inner expandOutUp' aria-hidden='false' style='position: absolute; left: "+x+"px; top: "+(y+33)+"px;'>\n" +
        "        <div class='color-group-wrapper color-dropdown-wrapper'>\n" +
        "            <ul class='color-group-panel'>" +
        "            <li class='color-tag active'>\n" +
        "                <a><div class='color-block' style='background: rgb(255, 255, 255) none repeat scroll 0% 0%;'>\n" +
        "                </div>\n" +
        "                    <span>白色系</span>\n" +
        "                </a>\n" +
        "            </li>\n" +
        "                <li class='color-tag'>\n" +
        "                    <a>\n" +
        "                        <div class='color-block' style='background: rgb(128, 128, 128) none repeat scroll 0% 0%;'></div>\n" +
        "                        <span>灰色系</span>\n" +
        "                    </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>\n" +
        "                    <a><div class='color-block' style='background: rgb(0, 0, 0) none repeat scroll 0% 0%;'></div>\n" +
        "                        <span>黑色系</span>\n" +
        "                    </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>\n" +
        "                    <a>\n" +
        "                        <div class='color-block' style='background: rgb(255, 0, 0) none repeat scroll 0% 0%;'></div>\n" +
        "                        <span>红色系</span>\n" +
        "                    </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>\n" +
        "                    <a>\n" +
        "                        <div class='color-block' style='background: rgb(255, 255, 0) none repeat scroll 0% 0%;'></div>\n" +
        "                        <span>黄色系</span>\n" +
        "                    </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>" +
        "                     <a>\n" +
        "                    <div class='color-block' style='background: rgb(0, 128, 0) none repeat scroll 0% 0%;'></div>\n" +
        "                    <span>绿色系</span>\n" +
        "                </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>\n" +
        "                    <a>\n" +
        "                        <div class='color-block' style='background: rgb(0, 0, 254) none repeat scroll 0% 0%;'></div>\n" +
        "                        <span>蓝色系</span>\n" +
        "                    </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>\n" +
        "                    <a>\n" +
        "                        <div class='color-block' style='background: rgb(128, 0, 128) none repeat scroll 0% 0%;'></div>\n" +
        "                        <span>紫色系</span>\n" +
        "                    </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>\n" +
        "                    <a>\n" +
        "                        <div class='color-block' style='background: rgb(124, 75, 0) none repeat scroll 0% 0%;'></div>\n" +
        "                        <span>棕色系</span>\n" +
        "                    </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>\n" +
        "                    <a>\n" +
        "                        <div class='color-block'></div>\n" +
        "                    <span>花色系</span>\n" +
        "                </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>" +
        "                    <a>\n" +
        "                    <div class='color-block'></div>\n" +
        "                    <span>透明系</span>\n" +
        "                </a>\n" +
        "                </li>\n" +
        "                <li class='color-tag'>" +
        "                     <a>\n" +
        "                    <div class='color-block'></div>\n" +
        "                    <span>其他</span>\n" +
        "                </a>\n" +
        "                </li>\n" +
        "            </ul>\n" +
        "            <div class='colors-panel'>\n" +
        "                <p class='colors-panel-title'>常用标准颜色</p>\n" +
        "                <ul class='color-list'>\n" +
        "                    <li class='color-tag'>\n" +
        "                        <a>\n" +
        "                            <div class='color-block' style='background: rgb(255, 251, 240) none repeat scroll 0% 0%;'></div>\n" +
        "                            <span>乳白色</span>\n" +
        "                        </a>\n" +
        "                    </li>\n" +
        "                    <li class='color-tag'>\n" +
        "                        <a>\n" +
        "                            <div class='color-block' style='background: rgb(238,222,176) none repeat scroll 0% 0%;'></div>\n" +
        "                            <span>米白色</span>\n" +
        "                        </a>\n" +
        "                    </li>\n" +
        "                    <li class='color-tag'>\n" +
        "                        <a>\n" +
        "                            <div class='color-block' style='background: rgb(255,255,255) none repeat scroll 0% 0%;'></div>\n" +
        "                            <span>白</span>\n" +
        "                        </a>\n" +
        "                    </li>\n" +
        "                    <li class='color-tag'>\n" +
        "                        <a>\n" +
        "                            <div class='color-block' style='background: rgb(255,255,255) none repeat scroll 0% 0%;'></div>\n" +
        "                            <span>白色</span>\n" +
        "                        </a>\n" +
        "                    </li>\n" +
        "                </ul>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>\n" +
        "</div>")
}

/**
 * 生成颜色输入框
 * @returns {*|Window.jQuery|HTMLElement}
 */
function generateColorInput() {
    return $("<li class='sell-color-checkbox-item'>\n" +
        "<div class='sell-color-checkbox-item-wrap'>\n" +
        "<div class='sell-color-checkbox-check-wrap'></div>\n" +
        "<div class='input-with-warning' aria-expanded='false' data-ismf='false'><span\n" +
        "class='next-input next-medium color-dropdown-input'><input\n" +
        "placeholder='选择或输入主色' height='100%' data-comment='' class='inputPrimary'></span></div><span\n" +
        " class='sell-o-input sell-color-checkbox-item-remark'><span\n" +
        "class='input-wrap'><span\n" +
        "class='next-input next-medium fusion-input'><input\n" +
        "placeholder='备注（如偏深偏浅等）'value='' class='comment' \n" +
        "height='100%'></span></span></span>\n" +
        "</div>\n" +
        "<div class='sell-o-message sell-color-checkbox-item-msg'>\n" +
        "</div>\n" +
        "</li>");
}

/**
 * 生成纯表格行
 * @returns {*|Window.jQuery|HTMLElement}
 */
function generateSkuTr(selectedColor) {
    return $("<tr class='sku-table-row'>\n" +
        "<td class='sell-sku-cell sell-sku-cell-image' rowspan='1' data-color='"+ selectedColor +"'>" +
        "<div class='cell-inner'><div class='sell-o-sku-images'>" +
        "<div class='sku-image-item-wrap'>" +
        "<div class='sku-image-wrap'>" +
        "</div>" +
        "<button title='选择图片' type='button' class='next-btn next-medium next-btn-primary next-btn-text action-link prompt-btn category-img-btn'>" +
        "<span class='next-btn-helper' data-spm-anchor-id='a213gs.14012020.0.i20.6c9f4831iaolY9'>选择图片" +
        "</span>" +
        "</button>" +
        "<input type='file' style='display: none' name='categoryImg' class='category-up-img' multiple='multiple'/>" +
        "<input type='hidden' value='' name='imgName' class='category-img-name'>"+
        "</div></div></div>" +
        "</td>" +
        "<td class='sell-sku-cell sell-sku-cell-money' rowspan='1'>\n" +
        "<div class='cell-inner' style='min-width: 80px;'><span class='sell-o-number'><span class='input-wrap'><span class='next-input next-medium fusion-input'><input value='' height='100%' type='number'></span></span></span>\n" +
        "</div>\n" +
        "</td>\n" +
        "<td class='sell-sku-cell sell-sku-cell-positiveNumber' rowspan='1'>\n" +
        "<div class='cell-inner' style='min-width: 80px;'><span class='sell-o-number'><span class='input-wrap'><span class='next-input next-medium fusion-input'><input value='' height='100%' type='number'></span></span></span>\n" +
        "</div>\n" +
        "</td>\n" +
        "</tr>")
}

// 点击上传或者替换分类图片
$("body").on("click", ".category-img-btn, .prompt-btn", function () {
    $(this).nextAll().click();
    $(".category-up-img").off("change").on("change",function(){
        var file = $(this);
        var fileSize = file[0].files[0].size;
        if(fileSize > 1024 * 1024 *3){
            // 显示消息
            return false;
        }
        // 获得跟这个同级别的所有图片表情
        var nowColor = $(this).parents(".sell-sku-cell-image").attr("data-color");
        var $img = $(".sell-sku-cell-image[data-color='"+nowColor+"'] .sku-image-wrap");
        $img.find(".sell-image").remove();
        $img.each(function () {
            var $img = $("<img class='sell-image' alt='' src=''>");
            $(this).append($img);
            html5Reader(file,$img);
        });
        var uuid = Totoro.getUUID();
        var pos = file.val().lastIndexOf("\\");
        var fileName =  "/static/img/product/"+uuid+file.val().substring(pos+1);
        // 修改文件名字
        var $box = $(".sell-sku-cell-image[data-color='"+nowColor+"'] .sku-image-item-wrap .category-img-name");
        $box.val(fileName);
        // 将此次上传图片改成替换删除
        var $update = $("<button title='替换' type='button' class='next-btn next-medium next-btn-primary next-btn-text action-link prompt-btn'><span class='next-btn-helper' data-spm-anchor-id='a213gs.14012020.0.i24.6c9f4831iaolY9'>替换</span></button>");
        var $delete = $("<button type='button' class='next-btn next-medium next-btn-primary next-btn-text action-link remove-link'><span class='next-btn-helper'>删除</span></button>")
        var $buttonBox = $(".sell-sku-cell-image[data-color='"+nowColor+"'] .sku-image-item-wrap");
        $buttonBox.find(".category-img-btn").remove();
        $buttonBox.find(".prompt-btn").remove();
        $buttonBox.find(".remove-link").remove();
        $buttonBox.find(".category-up-img").before($update);
        $buttonBox.find(".category-up-img").before($delete);
    })
});

// 删除分类图片
$("body").on("click", ".remove-link", function () {
    var nowColor = $(this).parents(".sell-sku-cell-image").attr("data-color");
    var $img = $(".sell-sku-cell-image[data-color='"+nowColor+"'] .sku-image-wrap");
    $img.find(".sell-image").remove();
    var $buttonBox = $(".sell-sku-cell-image[data-color='"+nowColor+"'] .sku-image-item-wrap");
    // 清空file input
    $buttonBox.find(".category-up-img").val("");
    $buttonBox.find(".prompt-btn").remove();
    $buttonBox.find(".remove-link").remove();
    $selected = $("<button title='选择图片' type='button' class='next-btn next-medium next-btn-primary next-btn-text action-link prompt-btn category-img-btn'>" +
        "<span class='next-btn-helper''>选择图片</span>" +
        "</button>");
    $buttonBox.find(".category-up-img").before($selected);
});

//H5渲染，上传图片
function html5Reader(file,pic) {
    var file = file[0].files[0];
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e) {
        pic.attr("src", this.result);
    }
}

/**
 * 生成sku属性对应的表格列
 * @returns {*|Window.jQuery|HTMLElement}
 */
function generateSkuTd(value){
    return $("<td class='sell-sku-cell sell-sku-cell-text' rowspan='1'>\n" +
        "<div class='cell-inner' style='min-width: 78px;'>\n" +
        "<div class='sell-sku-cell-text'>\n" +
        "<p class='sell-sku-cell-text-content' title='"+value+"'>"+value+"</p>\n" +
        "</div>\n" +
        "</div>\n" +
        "</td>");
}


/**
 * 生成普通自定义复选框选项
 * @param text
 * @returns {*|Window.jQuery|HTMLElement}
 */
function generateByValue(text) {
    return $("<div class='option-wrap' style='width: 112.571px;'>\n" +
        "<div class='sell-sequential-checkbox-content'><span class='next-checkbox'><span class='next-checkbox-inner'><i class='next-icon next-icon-select next-xs next-checkbox-select-icon'></i>\n" +
        "</span>\n" +
        "<input type='checkbox' name='c' lay-skin='primary' data-value='"+text+"'>" +
        "<div class='layui-unselect layui-form-checkbox' lay-skin='primary'><i class='layui-icon layui-icon-ok'></i></div>\n" +
        "</span>\n" +
        "<span class='next-checkbox-label'>\n" +
        "<span class='normal-content'>"+text+"</span>\n" +
        "</span>\n" +
        "</div>\n" +
        "</div>");
};

function refreshForm() {
    var form = layui.form;
    form.render('checkbox');
}


// 以下为取值----------------------------------------------

/**
 * 点击发布，取出所有的值
 */
$("#release").click(function () {
    // 宝贝标题

    var babyTitle = $("#inputTitle");

    // 填写的验证
    // 1、判断用户是否填写标题
    if(babyTitle.val().length<=0){
        tipMsg = "宝贝标题";
        tipEl = babyTitle;
        $("#tips").click();
        return false;
    }

    //2、判断用户类目属性的填写 （其中的必填项）
    var $mustFillCi = $(".sell-catProp-item[data-mustfill='1']");
    for (let i = 0; i < $mustFillCi.length; i++) {
        console.log($mustFillCi.eq(i).attr("data-havevalue"))
        if($mustFillCi.eq(i).attr("data-havevalue") == "false"){
            tipMsg = $mustFillCi.eq(i).find(".spu-title").text();
            tipEl = $mustFillCi.eq(i);
            $("#tips").click();
            return false;
        }
    }

    var $baby = $("#struct-stuffStatus").find("input:checked");
    var $global = $("#struct-globalStock").find("input:checked")
    // 3、判断用户宝贝类型是否填写
    if($baby.length<=0){
        tipMsg = "宝贝类型";
        tipEl = $("#struct-stuffStatus");
        $("#tips").click();
        return false;
    }
    if($global.length<=0){
        tipMsg = "采购地";
        tipEl = $("#struct-globalStock");
        $("#tips").click();
        return false;
    }

    // 是否判断sku，如果是虚拟物品，不判断
    if($("#virtual").val() == 'false') {
    //4 、判断宝贝重要sku属性是否填写 (1、获取该类别是否拥有sku属性，如果有，进行sku的复选框选中个数判断是否选中)
    var $allSku = $("#skuTable").find("div[id*='struct-p-']");
    if($allSku.length>0){
        for (let i = 0; i < $allSku.length; i++) {
            if($allSku.eq(i).find("input:checked").length<=0){
                tipMsg = "商品SKU属性";
                tipEl = $("#card-sale");
                $("#tips").click();
                return false;
            }
        }
    }

        // 判断用户是否每个sku都填写了价格与
        var $allSkuTr = $(".sku-table-row");
        for (let i = 0; i < $allSkuTr.length; i++) {
            var money = $allSkuTr.eq(i).find(".sell-sku-cell-money input").val();
            var count = $allSkuTr.eq(i).find(".sell-sku-cell-positiveNumber input").val();
            // 如果用户有未填写的价格或者数量
            if (money.length <= 0 || count.length <= 0) {
                tipMsg = "产品SKU属性";
                tipEl = $("#card-sale");
                $("#tips").click();
                return false;
            }
        }
    }

    //5 、判断一口价是否填写
    var $onePrice = $("#flatlyPrice")
    if($onePrice.val().length<=0){
        tipMsg = "宝贝一口价";
        tipEl = $("#card-pay");
        $("#tips").click();
        return false;
    }

    // 6、判断总数量是否大于0
    var $count = $("#count");
    if(parseInt($count.val()) <=0){
        tipMsg = "宝贝总数量";
        tipEl = $("#card-pay");
        $("#tips").click();
        return false;
    }


    var $stockCount = $("#struct-subStock").find("input:checked");
    // 7、判断支付信息库存计数是否填写好
    if($stockCount.length<=0){
        tipMsg = "库存计数";
        tipEl = $("#card-pay");
        $("#tips").click();
        return false;
    }

    var $statu = $("#struct-productStatus").find("input:checked");
    // 7、判断支付信息库存计数是否填写好
    if($stockCount.length<=0){
        tipMsg = "上架状态";
        tipEl = $("#card-pay");
        $("#tips").click();
        return false;
    }


    var $extractType = $("#struct-tbExtractWay").find("input:checked");
    // 8、判断提取方式是否填写好
    if($extractType.length<=0){
        tipMsg = "提取方式";
        tipEl = $("#card-deliver");
        $("#tips").click();
        return false;
    }


    var editorHtml = editor.getData();
    // 9、判断图文描述是否输入
    if(editorHtml.length <=0){
        tipMsg = "图文描述";
        tipEl = $("#card-desc");
        $("#tips").click();
        return false;
    }





    // spu 属性 json
    var spuObject = [];
    var spuIndex = -1;
    $(".all-items").children().each(function () {
        // 如果填写了值，才添加
        if($(this).attr("data-havevalue") == "true"){
            spuIndex++;
            var title,value;
            title = $(this).find(".spu-title").text();

            // 取值方式不相同，判断是下拉框，复选框，小复选框
            var $type = $(this).find(".content").children();
            if($type.hasClass("next-select")){
                value = $(this).find(".spu-value").val();
            }

            if($type.hasClass("next-checkbox")){
                value = $(this).find(".spu-value").text();
            }

            if($type.hasClass("next-smallCheckbox")){
                value = "";
                $(this).find(".spu-value *").each(function (index, el) {
                    // 如果选中了，才添加值
                    var $nowCk = $(this).find("input:checked");
                    if($nowCk.length>0){
                        value+= $nowCk.next().next().text()+",";
                    }
                });
                value = value.substring(0, value.length-1);
            }
            spuObject[spuIndex] = {"title":title,"value":value};
        }
    });

    // 获得宝贝类型和采购地的spu
    var $baby = $("#struct-stuffStatus").find("input:checked");
    var $global = $("#struct-globalStock").find("input:checked")
    spuObject[spuObject.length] = {"title":"宝贝类型","value":$baby.val()}
    spuObject[spuObject.length] = {"title":"采购地","value":$global.val()}

    // 转换json
    var spuJson = JSON.stringify(spuObject);

    console.log(spuJson)

    var skuObject = [];
    if($("#virtual").val() == 'false') {
        if ($allSku.length > 0) {
            // 获得sku属性大div
            var $one = $("#skuTable").find("div[id*='struct-p-']").eq(0).find("input:checked");
            var oneText = $("#skuTable").find("div[id*='struct-p-']").eq(0).find("label.label").text();
            // 取值思路：从开始到最后，循环所有的复选框个数，进行最多三重循环的操作 （取属性值不必要跟着表格，只需跟着已选择的复选框）
            // 1、获取所有sku属性选中的复选框个数
            // 循环删除没有的分类图input
            for (let i = 0; i < $one.length; i++) {
                var $input = $(".sell-sku-cell-image[data-color='"+i+"'] .category-up-img");
                var uuid = Totoro.getUUID();
                $input.each(function (id, el) {
                    var val = $(this).val();
                    if(val < 1){
                        $(this).next().remove();
                        $(this).remove();
                    }
                    // if(val.length > 0){
                    //     var pos = val.lastIndexOf("\\");
                    //     fileName =  uuid+val.substring(pos+1);
                    //     // 上传一波图片
                    //     // 判断图片类型
                    //     //判断文件类型,我这里根据业务需求判断的是Excel文件
                    //     var fileName1 = val.substring(val.lastIndexOf(".") + 1).toLowerCase();
                    //     fileNames.push(fileName1);
                    //     console.log("ok")
                    //     formData.append(type, $(this)[0].files[0]);
                    // } else{
                    //     $(this).remove();
                    // }
                });
                // 获取$color的下一级
                var $next1 = $one.eq(i).parents("div[id*='struct-p-']").next().find("input:checked");
                var next1Text = $one.eq(i).parents("div[id*='struct-p-']").next().find("label.label").text();
                if ($allSku.length > 1) {
                    for (let j = 0; j < $next1.length; j++) {
                        var $next2 = $next1.eq(j).parents("div[id*='struct-p-']").next().find("input:checked");
                        var next2Text = $next1.eq(j).parents("div[id*='struct-p-']").next().find("label.label").text();
                        if ($next1.eq(j).parents("div[id*='struct-p-']").next().length > 2) {
                            for (let k = 0; k < $next2.length; k++) {
                                var text1 = $one.eq(i).parent().next().find("input").val();
                                var text2 = $next1.eq(j).data("value");
                                var text3 = $next2.eq(k).data("value");
                                var param = {};
                                $imageName = $(".sell-sku-cell-image[data-color='"+i+"']");
                                var fileName = "";
                                $imageName.each(function () {
                                    if($(this).find(".category-img-name").length > 0){
                                        fileName = $(this).find(".category-img-name").val();
                                    }
                                })
                                param[oneText] = text1+(fileName.length>0?("|"+fileName):'');
                                param[next1Text] = text2;
                                param[next2Text] = text3;
                                skuObject.push(param);
                            }
                        } else {
                            var text1 = $one.eq(i).parent().next().find("input").val();
                            var text2 = $next1.eq(j).data("value");
                            var param = {};
                            $imageName = $(".sell-sku-cell-image[data-color='"+i+"']");
                            var fileName = "";
                            $imageName.each(function () {
                                if($(this).find(".category-img-name").length > 0){
                                    fileName = $(this).find(".category-img-name").val();
                                }
                            })
                            param[oneText] = text1+(fileName.length>0?("|"+fileName):'');
                            param[next1Text] = text2;
                            skuObject.push(param);
                        }
                    }
                } else {
                    var text1 = $one.eq(i).parent().next().find("input").val();
                    var param = {};
                    param[oneText] = text1;
                    skuObject.push(param);
                }
            }
            console.log(skuObject)
        }
    } else{
        var param = "[{";
        // 否则则是虚拟物品，spu当sku
        $(".all-items").children().each(function () {
            // 如果填写了值，才添加
            if ($(this).attr("data-havevalue") == "true") {
                var title,value;
                title = $(this).find(".spu-title").text();

                // 取值方式不相同，判断是下拉框，复选框，小复选框
                var $type = $(this).find(".content").children();
                if($type.hasClass("next-select")){
                    value = $(this).find(".spu-value").val();
                }

                if($type.hasClass("next-checkbox")){
                    value = $(this).find(".spu-value").text();
                }

                if($type.hasClass("next-smallCheckbox")){
                    value = "";
                    $(this).find(".spu-value *").each(function (index, el) {
                        // 如果选中了，才添加值
                        var $nowCk = $(this).find("input:checked");
                        if($nowCk.length>0){
                            value+= $nowCk.next().next().text()+",";
                        }
                    });
                    value = value.substring(0, value.length-1);
                }
                param+="'"+title+"':'"+value+"',";
            }
        });
        param = param.substring(0, param.length-1);
        param+="}]";
        skuObject.push(param);
        skuObject = eval("(" + skuObject +")");
    }
    // 转换json
    var skuJson = JSON.stringify(skuObject);



    var moneys = new Array(), counts = new Array();
    if($("#virtual").val() == 'false') {
        // 再获取商品的价格和数量，先获取所有的Tr
        for (let i = 0; i < $allSkuTr.length; i++) {
            var money = $allSkuTr.eq(i).find(".sell-sku-cell-money input").val();
            var count = $allSkuTr.eq(i).find(".sell-sku-cell-positiveNumber input").val();
            moneys.push(money);
            counts.push(count);
            //
        }
    } else{
        moneys.push($onePrice.val());
        counts.push($count.val());
    }


    // 支付信息  -- 庫存計數 库存计数不存在spujson里，而是当作该商品的字段
    var subStock = $stockCount.val();
    var status = $statu.val();

    // 获取提取方式 （并没什么用）
    var extracts = new Array();
    $extractType.each(function (index, el) {
        extracts.push($(this).val());
    });

    // 获得图文描述
    var editorHtml = editor.getData();

    // 将值传给fwd
        $.ajax({
            type:"POST",
            url:"/attr/publish.htm",
            async:false,
            data: {"title":babyTitle.val(), "spuJson":spuJson, "skuJson":skuJson, "moneys":moneys, "counts":counts, "subStock":subStock, "status":status, "extracts":extracts, "editorHtml":editorHtml, "cid":$("#getCid").val()},
            success:function (data) {
                $("#success").click();
                setTimeout( v=>{
                    $("#uploadForm").submit();
                }, 1000);
            }
        });

    $("#uploadForm").submit();
});



/**
 * 自动跳转元素位置
 * @param jqueryEl
 */
function jumpElPosition(jqueryEl) {
    var ctop = jqueryEl[0].offsetTop;
    $('html,body').animate({
        scrollTop: ctop
    },0);
}

// elementUI 事件
var Main = {
    methods: {
        tips() {
            this.$notify.error({
                title: '发布失败 - '+tipMsg+'未填写！',
                dangerouslyUseHTMLString: true,
                message: '<a style="color: red; font-size: 15px" id="jump">点击跳转至对应位置</a>',
                position: 'top-left',
                duration: 0,
                onClick: function () {
                    this.close()
                }
            });
        },
        success() {
            this.$alert('发布宝贝成功，正在为你跳转至你的宝贝列表', '发布商品', {
                confirmButtonText: '确定',
                callback: action => {
                    this.$message({
                        type: 'info',
                        message: `action: ${action}`
                    });
                }
            });
        },
    }
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app');

/**
 * 跳转元素高度统一方法
 */
$("body").on("click", "#jump", function () {
    jumpElPosition(tipEl);
})


